from DBUtils.PooledDB import PooledDB
import pymysql
import public
import logging

sql_log = logging.getLogger('sql')


class Mysql(object):
    __instance = None

    def __new__(cls, *args, **kwargs):
        if cls.__instance is None:
            cls.__instance = super(Mysql, cls).__new__(cls, *args, **kwargs)
        return cls.__instance

    def __init__(self):
        self.mysql = PooledDB(pymysql,public.initial_pool,host=public.host,user=public.user,passwd=public.pwd,db=public.dbname,port=public.port, charset="utf8")

    def getAll(self, sql):
        # print(id(self.mysql))
        _conn = self.mysql.connection()
        _cursor = _conn.cursor()
        _cursor.execute(sql)
        result = _cursor.fetchall()
        _cursor.close()
        _conn.close()
        return result

    def updateOne(self, sql):
        _conn = self.mysql.connection()
        _cursor = _conn.cursor()
        _cursor.execute(sql)
        _conn.commit()
        _cursor.close()
        _conn.close()
        return